﻿using rmTest.Common;
using rmTest.Common.ExtensionUtil;
using rmTest.Common.ModelUtil;
using rmTest.First.Model;
using rmTest.First.ServiceFactory;
using rmTest.First.ServiceInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProgramTest
{
    public static class AdoNetTestShow
    {
        public static void Show()
        {
            IBaseService service = SimpleFactory.CreateService();//用工厂方式创建服务，返回接口
            {
                #region 查询数据

                UserModel userModel = service.Get<UserModel>(2);
                UserModel userModel1 = service.Get<UserModel>(1);

                //获取所有User数据
                List<UserModel> listUsers = service.GetList<UserModel>();
                //获取id为1的user数据
                var user = service.Get<UserModel>(1);
                StringBuilder builder = new StringBuilder();
                //打印所有用户的信息
                foreach (var userItem in listUsers)
                {
                    InitBuilderByModel(builder, userItem);
                }
                builder.Append("User 信息显示完毕！" + "\t\n");
                ConsoleUtil.Output(builder);

                //Console.ForegroundColor = ConsoleColor.Red;  //设置字体颜色为红色
                //获取所有的company
                List<CompanyModel> listCompanys = service.GetList<CompanyModel>();
                //获取id为1的company
                var company = service.Get<CompanyModel>(1);
                #endregion
            }

            {
                #region 插入数据
                StringBuilder builder2 = new StringBuilder();
                UserModel userModel = new UserModel();
                userModel.Name = "微白4";
                userModel.Account = "weibai";
                userModel.Password = "e10adc3949ba59abbe56e057f20f883e";
                userModel.Email = "110244@qq.com";
                userModel.Mobile = "13121111111";
                userModel.CompanyId = 1;
                userModel.CompanyName = "百捷";
                //userModel.State = 2;
                //userModel.Type = 2;
                userModel.LastLoginTime = DateTime.Now;
                //userModel.CreateTime = DateTime.Now;
                //userModel.CreatorId = 1;
                userModel.LastModifierId = 1;
                //userModel.LastModifyTime = DateTime.Now;
                //InitBuilderByModel(builder2, userModel);
                //处理数据校验
                ValidateResult validate = userModel.Validate<UserModel>();
                if (validate.isValidated)
                {
                    int iexcute = service.InsertDataByModel<UserModel>(userModel);
                    builder2.AppendFormat("新增 User 数据：{0} 条", iexcute);
                }
                else
                {
                    builder2.Append("数据校验失败：\t\n");
                    foreach (var msgKeyValue in validate.ValidateMsg)
                    {
                        if (!string.IsNullOrEmpty(msgKeyValue.Value))
                        {
                            builder2.AppendFormat("\t 属性：{0}，错误信息：{1}\t\n", msgKeyValue.Key, msgKeyValue.Value);
                        }
                    }
                }

                //数据校验
                userModel.Name = "微白";
                userModel.State = 20;
                userModel.Type = -2;
                //处理数据校验
                validate = userModel.Validate<UserModel>(true);
                if (validate.isValidated)
                {
                }
                else
                {
                    builder2.Append("数据校验失败：\t\n");
                    foreach (var msgKeyValue in validate.ValidateMsg)
                    {
                        if (!string.IsNullOrEmpty(msgKeyValue.Value))
                        {
                            builder2.AppendFormat("\t 属性：{0}，错误信息：{1}\t\n", msgKeyValue.Key, msgKeyValue.Value);
                        }
                    }
                }
                ConsoleUtil.Output(builder2);
                #endregion
            }

            {
                #region 更新数据
                StringBuilder builder = new StringBuilder();
                UserModel userModel = new UserModel();
                userModel.Id = 6;
                userModel.Name = "微白6";
                userModel.Account = "weibai6";
                userModel.Password = "e10adc3949ba59abbe56e057f20f883e5";
                userModel.Email = "110244@qq.com";
                userModel.Mobile = "13121111111";
                userModel.CompanyId = 1;
                userModel.CompanyName = "百捷";
                userModel.State = 2;
                userModel.Type = 2;
                userModel.LastLoginTime = DateTime.Now;
                userModel.CreateTime = DateTime.Now;
                userModel.CreatorId = 1;
                userModel.LastModifierId = 1;
                userModel.LastModifyTime = DateTime.Now;
                int iexcute = service.UpdateDataByModel<UserModel>(userModel);
                builder.AppendFormat("更新 User id 为 {0} 数据：{1} 条\t\n", userModel.Id, iexcute);
                ConsoleUtil.Output(builder);
                #endregion
            }

            {
                #region 删除数据
                StringBuilder builder = new StringBuilder();
                UserModel userModel = new UserModel();
                userModel.Id = 6;

                int iexcute = service.Delete<UserModel>(userModel.Id);
                builder.AppendFormat("删除 User id 为 {0} 数据：{1} 条\t\n", userModel.Id, iexcute);
                ConsoleUtil.Output(builder);
                #endregion
            }
        }
        private static void InitBuilderByModel(StringBuilder builder, UserModel userItem)
        {
            builder.Append("User " + userItem.Id + " 相关属性：\t\n");
            var fields = typeof(UserModel).GetFields();
            foreach (var field in fields)
            {
                var fieldValue = field.GetValue(userItem);
                builder.Append("\t" + field.Name + "：" + fieldValue + "\t\n");
            }
            var propertys = typeof(UserModel).GetProperties();
            foreach (var property in propertys)
            {
                var propertyValue = property.GetValue(userItem);
                builder.Append("\t" + property.Name + "：" + propertyValue + "\n");
            }
        }
    }
}
